package com.example.droidwarehouse;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

import com.example.droidwarehouse.barcodescanner.IntentIntegrator;
import com.example.droidwarehouse.barcodescanner.IntentResult;
import com.example.droidwarehouse.config.Config;
import com.example.droidwarehouse.item.Item;
import com.example.droidwarehouse.item.ItemService;
import com.example.droidwarehouse.item.ItemService.ItemStatus;

/**
 * Main activity showing a scan button which starts scanner and decides what to
 * do with its result.
 * 
 * @author Bibek Shrestha <bibek.shrestha@tum.de>
 * @author David Schattel
 * @author Dansen Zhou
 * @since 15.11.2012
 */
public class MainActivity extends Activity
{
    final public static String TAG = "DroidWareHouse";

    private Button b_scan;
    private ItemService itemService;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Config.setContext(getApplicationContext());

        itemService = new ItemService();

        setContentView(R.layout.activity_main);
        // initialize scan button and wait for it to be tapped
        b_scan = (Button) findViewById(R.id.b_scan);
    }

    /**
     * Listener for main Scan button
     * 
     * @param view
     */
    public void btnScanClickListener(View view)
    {
        IntentIntegrator.initiateScan(this);
    }

    /**
     * Reaction on result of QR Barcode Scanner
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        switch (requestCode)
        {
        case IntentIntegrator.REQUEST_CODE:
        {
            if (resultCode != RESULT_CANCELED)
            {
                IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
                if (scanResult != null)
                {
                    String upc = scanResult.getContents();
                    Log.d(TAG, "Item id from scan :" + upc);
                    handleItemId(upc);
                }
            }
            break;
        }
        }
    }

    /**
     * Determine if item is registered, available or rented. Depending on this,
     * open specific activity.
     * 
     * @param itemId
     *            Scanned ID of the warehouse item
     */
    protected void handleItemId(String itemId)
    {
        if (itemId != null && !itemId.isEmpty())
        {
            Item item = new Item();
            item.setId(itemId);

            Log.d(TAG, "Get status for item id " + item);
            ItemStatus status = itemService.getStatus(item);

            if (status.equals(ItemStatus.UNKNOWN))
            {
                Log.d(TAG, "Unknown item, show Register Activity " + item);
                changeToRegister(item);
            }
            else if (status.equals(ItemStatus.AVAILABLE))
            {
                Log.d(TAG, "Known item, unrented, show Rent Activity " + item);
                changeToRent(item);
            }
            else if (status.equals(ItemStatus.RENTED))
            {
                Log.d(TAG, "Known item, rented, show Retrieve Activity " + item);
                changeToRetrieve(item);
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    /**
     * Change the activity to Register Activity
     * 
     * @param item
     *            item object with itemid and itemname that will be registered
     */
    private void changeToRegister(Item item)
    {
        Intent i = new Intent();
        i.setClass(MainActivity.this, RegisterActivity.class);
        i.putExtra("itemId", item.getId());
        i.putExtra("itemName", item.getName());
        startActivity(i);
        MainActivity.this.finish();
    }

    /**
     * Change the activity to Rent Activity
     * 
     * @param item
     *            Item object with itemid which will be changed from available
     *            to onrent status
     */
    private void changeToRent(Item item)
    {
        Intent i = new Intent();
        i.setClass(MainActivity.this, RentActivity.class);
        i.putExtra("itemId", item.getId());
        i.putExtra("itemName", item.getName());
        startActivity(i);
        MainActivity.this.finish();
    }

    /**
     * Change the activity to Retrieve Activity
     * 
     * @param item
     *            Item object with itemid which will be changed from onrent to
     *            available status
     */
    private void changeToRetrieve(Item item)
    {
        Intent i = new Intent();
        i.setClass(MainActivity.this, RetrieveActivity.class);
        i.putExtra("itemId", item.getId());
        i.putExtra("itemName", item.getName());
        startActivity(i);
        MainActivity.this.finish();
    }
}
